class Stack {
    constructor () {
        this.items = []
    }

    // 元素压入栈
    push (ele) {
        this.items.push(ele)
    }

    // 将元素弹出栈
    pop () {
        return this.items.pop()
    }

    peek () {
        return this.items[this.items.length - 1]
    }

    isEmpty () {
        return !this.items.length
    }

    size () {
        return this.items.length
    }

    toString () {
        let resultString = ''
        for (let i = 0, len = this.items.length; i < len; i++) {
            resultString += this.items[i] + ' '
        }
        return resultString
    }
}

export default Stack

// 栈的应用
// 十进制转二进制
function dec2bin (decNumber) {
    let s = new Stack()
    while (decNumber > 0) {
        s.push(decNumber % 2)
        decNumber = Math.floor(decNumber / 2)
    }

    let binNumber = ''
    while (s.size()) {
        binNumber = binNumber + s.pop()
    }
    return binNumber
}
